﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Xml.Serialization;
using OutSource.Common.MarketSense;

/// <summary>
/// Summary description for ProductDatabase
/// </summary>
namespace QianJin.Models
{
    public partial class ProductDatabaseCategory
    {
        public string Name
        {
            get
            {
                string mkt = MarketSense.CurrentMarket.Name;

                switch (mkt.ToLower())
                {
                    case "zh-cn":
                        return this.zhcn.name;
                    case "en-us":
                        return this.enus.name;
                    default:
                        return this.enus.name;
                }
            }
        }
    }

    public partial class ProductDatabaseProduct
    {
        public string Name
        {
            get
            {
                string mkt = MarketSense.CurrentMarket.Name;

                switch (mkt.ToLower())
                {
                    case "zh-cn":
                        return this.zhcn.name;
                    case "en-us":
                        return this.enus.name;
                    default:
                        return this.enus.name;
                }
            }
        }

        public string Price
        {
            get
            {
                string mkt = MarketSense.CurrentMarket.Name;

                switch (mkt.ToLower())
                {
                    case "zh-cn":
                        return this.zhcn.price;
                    case "en-us":
                        return this.enus.price;
                    default:
                        return this.enus.price;
                }
            }
        }

        public bool HasBriefIntroduction
        {
            get
            {
                string mkt = MarketSense.CurrentMarket.Name;

                switch (mkt.ToLower())
                {
                    case "zh-cn":
                        return this.HasZHCNBriefIntroduction;
                    case "en-us":
                        return this.HasENUSBriefIntroduction;
                    default:
                        return this.HasENUSBriefIntroduction;
                }
            }
        }

        public bool HasENUSBriefIntroduction
        {
            get;
            set;
        }

        public bool HasZHCNBriefIntroduction
        {
            get;
            set;
        }
    }


    public partial class ProductDatabase
    {
        private static ProductDatabase database;

        public static ProductDatabase Instance
        {
            get
            {
                return database;
            }
        }

        private ProductDatabase() { }

        public static void Init(string xmlFile, string rootPath)
        {
            using (FileStream fs = new FileStream(xmlFile, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer xs = new XmlSerializer(typeof(ProductDatabase));
                database = (ProductDatabase)xs.Deserialize(fs);
            }

            if (database.category == null)
            {
                database.category = new ProductDatabaseCategory[0];
            }

            if (database.product == null)
            {
                database.product = new ProductDatabaseProduct[0];
            }

            foreach (var category in database.category)
            {
                if (category.product == null)
                {
                    category.product = new string[0];
                }
            }

            foreach (var prod in database.product)
            {
                string pathenus = string.Format(CultureInfo.InvariantCulture,
                "{0}products\\{1}\\en-us\\intro.htm", rootPath, prod.id);

                string pathzhcn = string.Format(CultureInfo.InvariantCulture,
                "{0}products\\{1}\\zh-cn\\intro.htm", rootPath, prod.id);

                prod.HasENUSBriefIntroduction = File.Exists(pathenus);
                prod.HasZHCNBriefIntroduction = File.Exists(pathzhcn);
            }
        }

        public ProductDatabaseProduct GetProduct(string id)
        {
            var products = from prod in ProductDatabase.Instance.product
                           where prod.id.Equals(id, StringComparison.OrdinalIgnoreCase)
                           select prod;

            return products.First();
        }

        public ProductDatabaseCategory[] GetAllCategories()
        {
            return ProductDatabase.Instance.category;
        }

        public ProductDatabaseProduct[] GetAllProductsFromCategory(string categoryId)
        {
            var category = 
                ProductDatabase.Instance.category.First(
                cat => 
                { 
                    return cat.id.Equals(categoryId, StringComparison.OrdinalIgnoreCase); 
                });

            var products = from prodId in category.product
                           select this.GetProduct(prodId);

            return products.ToArray();

        }

        public ProductDatabaseCategory GetCategory(string categoryId)
        {
            return ProductDatabase.Instance.category.First(
                cat => 
                { 
                    return cat.id.Equals(categoryId, StringComparison.OrdinalIgnoreCase); 
                });
        }
    }
}